package util;

import java.nio.ByteBuffer;

public class RemoteAddress {
	
	public static String readIP(ByteBuffer buff) {
		StringBuilder sb = new StringBuilder();
		sb.append(getNextPortion(buff));
		for(int i = 0; i<3; i++){
			sb.append("." + getNextPortion(buff));
		}
		sb.append(":" + (0xFFFF & buff.getShort()));
		return sb.toString();
	}
	
	private static int getNextPortion(ByteBuffer buff) {
		byte b = buff.get();
		return 0xFF & b;
	}

	public static void writeIP(String ip, ByteBuffer buffer) {
		String[] ipAndPort = ip.split(":");
		String[] ipBytes = ipAndPort[0].split("\\.");
		for(String s : ipBytes){
			buffer.put((byte)Integer.parseInt(s));
		}
		buffer.putShort((short)Integer.parseInt(ipAndPort[1]));
	}

}
